/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Mar 15, 2012  2:28:46 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.sales
 * File Name : BusinessPartnerServices.java
 * TODO
 */
package jpa.service.promis.sales;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.sales.BusinessPartner;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class BusinessPartnerServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(BusinessPartnerServices.class);

	/**
	 * @param em
	 */
	public BusinessPartnerServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	

	public BusinessPartner findById(String id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "BusinessPartner.findById"));				
	}
	
	public BusinessPartner findByCustomerNo(String customerCode) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("customerCode", customerCode);
		
		List<BusinessPartner> lst = super.ConvertToYoureObject(super.find("BusinessPartner.findByCustomerNo", mapWhereValue));
		if(!CommonService.IsNull(lst))
			if(lst.size() == 1)
				return lst.get(0);
			else			
				throw new Exception("Error Customer Code = "+ CommonService.IsNullRString(customerCode) +" has more "+ lst.size() +" record(s). ");
		else 
			return null;
	}
	
	public BusinessPartner findBySapPoNo(String syncExternalRefNo, String status) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("syncExternalRefNo", syncExternalRefNo);
		mapWhereValue.put("status", status);
		
		List<BusinessPartner> lst = super.ConvertToYoureObject(super.find("BusinessPartner.findBySapPoNo", mapWhereValue));
		if(!CommonService.IsNull(lst))
			return lst.get(0);
		else 
			return null;
	}
	
	public BusinessPartner InsertedOrUpdated(BusinessPartner vlaue) throws Exception {
		BusinessPartner obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{					
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public BusinessPartner Updated(BusinessPartner vlaue) throws Exception {
		BusinessPartner obj = vlaue;
		try{
			
			if(vlaue.getBpCode() != null)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}				
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<BusinessPartner> Inserted(List<BusinessPartner> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(BusinessPartner obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<BusinessPartner> Updated(List<BusinessPartner> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(BusinessPartner obj : vlauelist)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				common_lst.add(obj);	
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

}
